<?php

class ContentController extends BaseController {
    public function init() {
        parent::init();
        setLanguage(isset($_GET['lang']) ? $_GET['lang'] : null);
    }

    public function actionIndex() {
        $this->render('index');
    }

    public function actionArticle() {
        $article = Article::model()->findbypk($_GET['id']);
        $comment = $comment_list = null;
	
	if ($article == null) {
	    throw new CHttpException(404, t(404, 'messages'));
	}
	
	if (isset($article)) {            
            
            if(!$article->cat->status || $article->status!=1)
            {
                $this->render('/shared/_error',array('error'=>'Article not found'));
                app()->end();
            }
            
            $comment = Comment::model();
	    $comment_list = $comment->GetListByArticleId($article->id);
	    if (isset($_POST['Comment'])) {
		$comment = new Comment();
		$comment->attributes = $_POST['Comment'];
		if ($comment->validate()) {
		    $comment->create_date = date(LGConstant::DATE_FORMAT_FOR_DATABASE);
		    $comment->status = 0;
		    if ($comment->save()) {
			Yii::app()->user->setFlash('success', t('MSG_COMMENT_SUCCESS', 'content'));
			
			//Delete data when user send successfully
			$comment = new Comment();
		    } else {
			Yii::app()->user->setFlash('error', t('MSG_COMMENT_ERROR', 'content'));
		    }
		}		
	    }            
	}
        $this->render('article', array('article' => $article, 'comment' => $comment, 'comment_list' => $comment_list));
    }

    public function actionBlog() {
	$category = Category::model()->findbypk($_GET['id']);
	
	if ($category == null) {
	    throw new CHttpException(404, t(404, 'messages'));
	}
	
        $article = Article::model()->getListByCategoryId($_GET['id'],Yii::app()->session['lang'], LGConstantArticleStatus::PUBLISHED, LGConstant::PAGE_SIZE);
        
        if(!$category->status)
        {            
            $this->render('/shared/_error',array('error'=>'Category not found'));
            app()->end();
        }
        $this->render('blog', array('category' => $category, 'article' => $article));
    }
}